package test;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.util.Vector;

public class JtableDome extends JFrame {
    public JtableDome(){
        super("Jtable测试");
        Vector<Vector<Object>> data=new Vector<>();

        Vector<Object> rowVector1=new Vector<>();
        rowVector1.addElement("1");
        rowVector1.addElement("张三");
        rowVector1.addElement("甘肃");
        rowVector1.addElement("10");
        rowVector1.addElement("20");
        rowVector1.addElement("30");
        rowVector1.addElement("60");

        Vector<Object> rowVector2=new Vector<>();
        rowVector2.addElement("2");
        rowVector2.addElement("李四");
        rowVector2.addElement("安徽");
        rowVector2.addElement("40");
        rowVector2.addElement("20");
        rowVector2.addElement("30");
        rowVector2.addElement("90");

        Vector<Object> rowVector3=new Vector<>();
        rowVector3.addElement("3");
        rowVector3.addElement("王五");
        rowVector3.addElement("青海");
        rowVector3.addElement("30");
        rowVector3.addElement("40");
        rowVector3.addElement("30");
        rowVector3.addElement("100");

        data.addElement(rowVector1);
        data.addElement(rowVector2);
        data.addElement(rowVector3);
        //tablemodel和jtable关联之后只需要更新model就能把数据变化反映到jtable中

/**DefaultTableModel tableModel=new DefaultTableModel();
 tableModel.setDataVector(data,columns);//把数据和表头放进model中
 *
 */

        //jtable和table关联以后
        StudentTableModel studentTableModel=StudentTableModel.assembleModel(data);

        JTable jTable=new JTable(studentTableModel);//关联jtable和table

        JTableHeader tableHeader=jTable.getTableHeader();
        tableHeader.setFont(new Font(null,Font.BOLD,16));
        tableHeader.setForeground(Color.RED);
        //设置表格体
        jTable.setFont(new Font(null,Font.PLAIN,14));
        jTable.setForeground(Color.black);
        jTable.setGridColor(Color.BLACK);
        jTable.setRowHeight(30);

        //设置多行选择
        jTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );
        jTable.getTableHeader().setReorderingAllowed(false);//避免拖拽交换每一列

        //设置表格列的渲染方式
        Vector<String> colums= StudentTableModel.getColumns();
        StudentCellRender render=new StudentCellRender();
        for (int i = 0; i < colums.size(); i++) {
            TableColumn column=jTable.getColumn(colums.get(i));
            column.setCellRenderer(render);
            column.setResizable(false);
            if(i==0){
                column.setPreferredWidth(45);
                column.setMaxWidth(45);
            }
        }

        //jtable直接放在jpanel上会不带列头，需要jtable先放在滚动面板上。
        Container contentPane=getContentPane();//滚动面板再放在面板上
        JScrollPane jScrollPane=new JScrollPane(jTable);//滚动面板
        contentPane.add(jScrollPane);



        setSize(600,400);//窗体大小
        setLocationRelativeTo(null);//是否居中
        setResizable(false);//是否可以缩放
        setDefaultCloseOperation(EXIT_ON_CLOSE);//是否自动关闭程序
        setVisible(true);
    }
    public static void main(String[] args) {
        new JtableDome();
    }
}
class StudentTableModel extends DefaultTableModel{
    static Vector<String> columns=new Vector<>();
    static {
        columns.addElement("编号");
        columns.addElement("姓名");
//        columns.addElement("家乡");
        columns.addElement("语文");
        columns.addElement("数学");
        columns.addElement("英语");
        columns.addElement("总分");
    }
    private StudentTableModel(){// 单例模式
        super(null,columns);// data,列名
    }
    private static StudentTableModel studentTableModel=new StudentTableModel();
    public static StudentTableModel assembleModel(Vector<Vector<Object>> data){
        studentTableModel.setDataVector(data,columns);
        return studentTableModel;

    }

    public static Vector<String> getColumns() {
        return columns;
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;// 表示不可修改单元格里的内容1
    }
}

class StudentCellRender extends DefaultTableCellRenderer {
    // 每一行的每一列显示之前都会调用
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        if(row%2==0){
            setBackground(Color.LIGHT_GRAY);
        }else {
            setBackground(Color.WHITE);
        }
        setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }
}